package com.aaa.vip.service.impl;

import com.aaa.common.entity.About;
import com.aaa.common.entity.User;
import com.aaa.common.util.TokenUtil;
import com.aaa.vip.dao.AboutDao;
import com.aaa.vip.service.AboutService;
import com.aaa.vip.service.RemoteUserService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.vo.Page;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 关于表(About)表服务实现类
 *
 * @author makejava
 * @since 2025-09-19 18:11:16
 */
@Service("aboutService")
public class AboutServiceImpl implements AboutService {
    @Resource
    private AboutDao aboutDao;
    @Resource
    private HttpServletRequest request;
    @Resource
    private RemoteUserService remoteUserService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public About queryById(Integer id) {
        return this.aboutDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<About> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        About about = page.getData();
        List<About> abouts = aboutDao.queryAllByLimit(about);

        for (About about1 : abouts) {
            if (about1.getUpdateBy()!=null){
                about1.setUserB(remoteUserService.queryById(about1.getUpdateBy()));
            }
            if (about1.getCreateBy()!=null){
                about1.setUserA(remoteUserService.queryById(about1.getCreateBy()));
            }
        }
        return new PageInfo(abouts);
    }

    /**
     * 新增数据
     *
     * @param about 实例对象
     * @return 实例对象
     */
    @Override
    public About insert(About about) {
        String userId = TokenUtil.getUserId(request);
        about.setCreateBy(Integer.valueOf(userId));
        about.setUpdateBy(Integer.valueOf(userId));
        about.setIsvalid("0");
        this.aboutDao.insert(about);
        return about;
    }

    /**
     * 修改数据
     *
     * @param about 实例对象
     * @return 实例对象
     */
    @Override
    public About update(About about) {
        String userId = TokenUtil.getUserId(request);
        about.setUpdateBy(Integer.valueOf(userId));
        this.aboutDao.update(about);
        return this.queryById(about.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.aboutDao.deleteById(id) > 0;
    }
}
